from pygame_functions import *
import random

sd_possible = [-1, --1, -3, -4, -5, -6, 1, -1, 3, 4, 5, 6]
table_deck = []
current_turn = 1
last_drawn = -1
p1_total = 0
p2_total = 0
p1_standing = False
p2_standing = False
p1_busted = False
p2_busted = False
p1_wins = 0
p2_wins = 0
p1_side_deck = [sd_possible[random.randint(0, len(sd_possible)-1)] for card in range(0, 4)]
p2_side_deck = [sd_possible[random.randint(0, len(sd_possible)-1)] for card in range(0, 4)]
p1_drawn_previously = []
p2_drawn_previously = []

# Set Screen size and background color
screenSize(800, 800)
setBackgroundColour("white")

# Create the labels to show and hide
player_label = makeLabel(f"It is Player {current_turn}'s turn", 30, 0, 60)
options_label = makeLabel("[1] Hit [2] Stand [3] Side Deck", 30, 0, 90)
side_deck_label = makeLabel("Select Which Card to Play ([0],[1], etc)", 30, 0, 90)
drew_label = makeLabel(f"Player {current_turn} drew a {last_drawn}", 30, 0, 90)
p1_score_label = makeLabel(f"Player 1 Total: {p1_total}", 30, 0, 0)
p2_score_label = makeLabel(f"Player 2 Total: {p1_total}", 30, 0, 30)
p1_win_round_label = makeLabel("Player 1 Wins the Round", 30, 0, 0)
p2_win_round_label = makeLabel("Player 2 Wins the Round", 30, 0, 0)
p1_busted_label = makeLabel("Player 1 Has Busted", 30, 0, 30)
p2_busted_label = makeLabel("Player 2 Has Busted", 30, 0, 30)
p1_game_won = makeLabel("Player 1 Wins the Game!", 50, 150, 300)
p2_game_won = makeLabel("Player 2 Wins the Game!", 50, 150, 300)

# Create sprites of card images
# Black cards are positive
def num_to_sprite(num):
    if num == 1:
        return makeSprite("cards/b_01.png")
    elif num == 2:
        return makeSprite("cards/b_02.png")
    elif num == 3:
        return makeSprite("cards/b_03.png")
    elif num == 4:
        return makeSprite("cards/b_04.png")
    elif num == 5:
        return makeSprite("cards/b_05.png")
    elif num == 6:
        return makeSprite("cards/b_06.png")
    elif num == 7:
        return makeSprite("cards/b_07.png")
    elif num == 8:
        return makeSprite("cards/b_08.png")
    elif num == 9:
        return makeSprite("cards/b_09.png")
    elif num == 10:
        return makeSprite("cards/b_10.png")
    elif num == -1:
        return makeSprite("cards/r_01.png")
    elif num == -2:
        return makeSprite("cards/r_02.png")
    elif num == -3:
        return makeSprite("cards/r_03.png")
    elif num == -4:
        return makeSprite("cards/r_04.png")
    elif num == -5:
        return makeSprite("cards/r_05.png")
    elif num == -6:
        return makeSprite("cards/r_06.png")

p1_sd_sprites = []
p2_sd_sprites = []
for card in p1_side_deck:
    p1_sd_sprites.append(num_to_sprite(card))
for card in p2_side_deck:
    p2_sd_sprites.append(num_to_sprite(card))

def reset_round():
    global current_turn
    global p1_total
    global p2_total
    global p1_standing
    global p2_standing
    global p1_busted
    global p2_busted
    global p1_drawn_previously
    global p2_drawn_previously

    hideLabels()
    current_turn = 1
    p1_total = 0
    p2_total = 0
    p1_standing = False
    p2_standing = False
    p1_busted = False
    p2_busted = False
    p1_drawn_previously = []
    p2_drawn_previously = []
    for i in range(4):
        for j in range(10):
            table_deck.append(j+1)
    update_labels(1)
    showLabel(player_label)
    showLabel(options_label)
    showLabel(p1_score_label)
    showLabel(p2_score_label)
    xmod = 0
    for sprite in p1_sd_sprites:
        moveSprite(sprite, 100 + xmod * 52, 400)
        showSprite(sprite)
        transformSprite(sprite, 0, 2)
        xmod += 1
    xmod = 0
    for sprite in p2_sd_sprites:
        moveSprite(sprite, 400 + xmod * 52, 400)
        showSprite(sprite)
        transformSprite(sprite, 0, 2)
        xmod += 1

def hideLabels():
    hideLabel(player_label)
    hideLabel(options_label)
    hideLabel(side_deck_label)
    hideLabel(drew_label)
    hideLabel(p1_score_label)
    hideLabel(p2_score_label)
    hideLabel(p1_win_round_label)
    hideLabel(p2_win_round_label)
    hideLabel(p1_busted_label)
    hideLabel(p2_busted_label)

def update_labels(turn):
    changeLabel(player_label, f"It is Player {turn}'s turn")
    changeLabel(p1_score_label, f"Player 1 Total: {p1_total}")
    changeLabel(p2_score_label, f"Player 2 Total: {p2_total}")
    changeLabel(drew_label, f"Drew a {last_drawn}")

def draw_cards():
    for i in range(len(p1_drawn_previously)-1):
        if i % 2 == 0:
            moveSprite(p1_drawn_previously[i], 30, 150)
            showSprite(p1_drawn_previously[i])
    for i in range(len(p2_drawn_previously)-1):
        if i % 2 == 0:
            moveSprite(p2_drawn_previously[i], 30, 450)
            showSprite(p2_drawn_previously[i])

def hit(turn):
    hideLabels()
    showLabel(p1_score_label)
    showLabel(p2_score_label)
    global p1_total
    global p2_total
    global p1_busted
    global p2_busted
    global last_drawn
    global p1_drawn_previously
    global p2_drawn_previously
    index = random.randint(0, len(table_deck)-1)
    last_drawn = table_deck[index]
    del table_deck[index]
    if turn == 1:
        p1_total += last_drawn
        p1_drawn_previously.append(num_to_sprite(last_drawn))
        update_labels(2)
        if p1_total > 20:
            p1_busted = True
    elif turn == 2:
        p2_total += last_drawn
        p2_drawn_previously.append(num_to_sprite(last_drawn))
        update_labels(1)
        if p2_total > 20:
            p2_busted = True
    showLabel(drew_label)
    waitPress()

def play_side_card(turn):
    global p1_sd_sprites
    global p2_sd_sprites
    global p1_total
    global p2_total
    global p1_side_deck
    global p2_side_deck
    hideLabels()
    showLabel(p1_score_label)
    showLabel(p2_score_label)
    showLabel(side_deck_label)
    if turn  == 1:
        choice = waitPress()
        if choice == 49:
            p1_total += p1_side_deck[0]
            del p1_side_deck[0]
            hideSprite(p1_sd_sprites[0])
            del p1_sd_sprites[0]
        elif choice == 50:
            p1_total += p1_side_deck[1]
            del p1_side_deck[1]
            hideSprite(p1_sd_sprites[1])
            del p1_sd_sprites[1]
        elif choice == 51:
            p1_total += p1_side_deck[2]
            del p1_side_deck[2]
            hideSprite(p1_sd_sprites[2])
            del p1_sd_sprites[2]
        elif choice == 52:
            p1_total += p1_side_deck[3]
            del p1_side_deck[3]
            hideSprite(p1_sd_sprites[3])
            del p1_sd_sprites[3]
        update_labels(2)
    elif turn  == 2:
        choice = waitPress()
        if choice == 49:
            p2_total += p2_side_deck[0]
            del p2_side_deck[0]
            hideSprite(p2_sd_sprites[0])
            del p2_sd_sprites[0]
        elif choice == 50:
            p2_total += p2_side_deck[1]
            del p2_side_deck[1]
            hideSprite(p2_sd_sprites[1])
            del p2_sd_sprites[1]
        elif choice == 51:
            p2_total += p2_side_deck[2]
            del p2_side_deck[2]
            hideSprite(p2_sd_sprites[2])
            del p2_sd_sprites[2]
        elif choice == 52:
            p2_total += p2_side_deck[3]
            del p2_side_deck[3]
            hideSprite(p2_sd_sprites[3])
            del p2_sd_sprites[3]
        update_labels(1)

reset_round()
while(True):
    tick(30)

    if p1_busted:
        hideLabels()
        showLabel(p1_busted_label)
        showLabel(p2_win_round_label)
        p2_wins += 1
        waitPress()
        reset_round()

    elif p2_busted:
        hideLabels()
        showLabel(p2_busted_label)
        showLabel(p1_win_round_label)
        p1_wins += 1
        waitPress()
        reset_round()

    if p2_wins == 3:
        hideLabels()
        showLabel(p2_game_won)
        waitPress()
        end()

    elif p1_wins == 3:
        hideLabels()
        showLabel(p1_game_won)
        waitPress()
        end()

    if not p1_standing:
        key = waitPress()
        if key == 49:
            hit(1)
            hideLabels()
            showLabel(p1_score_label)
            showLabel(p2_score_label)
            showLabel(options_label)
            showLabel(player_label)
        elif key == 50:
            p1_standing = True
            update_labels(2)
            hideLabels()
            showLabel(p1_score_label)
            showLabel(p2_score_label)
            showLabel(options_label)
            showLabel(player_label)

        elif key == 51:
            play_side_card(1)
            hideLabels()
            showLabel(p1_score_label)
            showLabel(p2_score_label)
            showLabel(options_label)
            showLabel(player_label)
    else:
        update_labels(2)


    if not p2_standing:
        key = waitPress()
        if key == 49:
            hit(2)
            hideLabels()
            showLabel(p2_score_label)
            showLabel(p1_score_label)
            showLabel(options_label)
            showLabel(player_label)
        elif key == 50:
            p2_standing = True
            update_labels(1)
            hideLabels()
            showLabel(p1_score_label)
            showLabel(p2_score_label)
            showLabel(options_label)
            showLabel(player_label)
        elif key == 51:
            play_side_card(2)
            hideLabels()
            showLabel(p1_score_label)
            showLabel(p2_score_label)
            showLabel(options_label)
            showLabel(player_label)
    else:
        update_labels(1)

    if p1_standing and p2_standing:
        if p1_total > p2_total:
            p1_wins += 1
            hideLabels()
            showLabel(p1_win_round_label)
            waitPress()
            reset_round()

        elif p2_total > p1_total:
            p2_wins += 1
            hideLabels()
            showLabel(p2_win_round_label)
            waitPress()
            reset_round()

        else:
            reset_round()

